package com.xcc.shopanim.newa;

import android.animation.ValueAnimator;
import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.makeramen.roundedimageview.RoundedImageView;
import com.xcc.shopanim.R;
import com.xcc.shopanim.adapter.SuperRecyclerAdapter;
import com.xcc.shopanim.mode.BannerMode;
import com.xcc.shopanim.mode.NewMainVideoMode;
import com.xcc.shopanim.utils.CenterLayoutManager;
import com.xcc.shopanim.utils.GlideUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

public class LayoutDrawerViewHolder {
    public LayoutDrawerViewHolder(Activity activity, View drawerLayout, List<NewMainVideoMode> modeList) {
        this.activity = activity;
        this.modeList = modeList;

        Resources res = activity.getResources();
        qb_px_12 = (int) res.getDimension(R.dimen.qb_px_12);

        detailsModeMap = new HashMap<>(15);

        ButterKnife.bind(this, drawerLayout);

        adapter = new LayoutDrawerAdapter(activity, modeList);
        recyclerView.setAdapter(adapter);
        recyclerView.setLayoutManager(layoutManager = new CenterLayoutManager(activity, LinearLayoutManager.HORIZONTAL, false));
    }

    private Activity activity;
    private List<NewMainVideoMode> modeList;
    private LayoutDrawerAdapter adapter;
    private LinearLayoutManager layoutManager;
    private OnLayoutDrawerListener onLayoutDrawerListener;
    private Map<NewMainVideoMode, Object> detailsModeMap;
    private boolean open;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;

    public LayoutDrawerViewHolder setOnLayoutDrawerListener(OnLayoutDrawerListener onLayoutDrawerListener) {
        this.onLayoutDrawerListener = onLayoutDrawerListener;
        return this;
    }

    public void notifyDataSetChanged() {
        adapter.notifyDataSetChanged();
    }

    public void onPageSelected(int position) {
        final int oldPos = this.selectPosition;
        this.selectPosition = position;
        if (position == 0 || position + 1 == modeList.size()) {
            layoutManager.scrollToPosition(position);
        } else {
            layoutManager.smoothScrollToPosition(recyclerView, new RecyclerView.State(), position);
        }
        setViewBg(oldPos);
        setViewBg(position);
    }

    private void setViewBg(int position) {
        View view = layoutManager.findViewByPosition(position);
        if (view == null) return;
        RecyclerView.ViewHolder childViewHolder = recyclerView.getChildViewHolder(view);
        if (childViewHolder instanceof SuperRecyclerAdapter.ViewHolder) {
            SuperRecyclerAdapter.ViewHolder holder = (SuperRecyclerAdapter.ViewHolder) childViewHolder;
            holder.setPosition(position);
        }
    }

    public void initRecyclerView() {
        int height = recyclerView.getHeight();
        setViewBottom(recyclerView, -height);
    }

    public void changeOpen() {
        open = !open;

        final int height = recyclerView.getHeight();
        ValueAnimator animator;
        if (open) animator = ValueAnimator.ofInt(-height, qb_px_12);
        else animator = ValueAnimator.ofInt(qb_px_12, -height);
        animator.setDuration(300);
        animator.addUpdateListener(animation -> {
            Object animatedValue = animation.getAnimatedValue();
            if (animatedValue instanceof Integer) {
                int v = (Integer) animatedValue;
                setViewBottom(recyclerView, v);
                if (onLayoutDrawerListener == null) return;
                onLayoutDrawerListener.onContentOffset(height + v);
            }
        });
        animator.start();

        if (onLayoutDrawerListener == null) return;
        onLayoutDrawerListener.onChangeOpen(open);
    }

    private void setViewBottom(View view, int bottom) {
        if (view == null) return;
        ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
        if (layoutParams instanceof ViewGroup.MarginLayoutParams) {
            ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) layoutParams;
            params.bottomMargin = bottom;
            view.setLayoutParams(params);
        }
    }

    public class LayoutDrawerAdapter extends SuperRecyclerAdapter<NewMainVideoMode> {
        public LayoutDrawerAdapter(Context context, List<NewMainVideoMode> modeList) {
            super(context, modeList);
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_main_live_toplist;
        }

        @Override
        public ViewHolder<NewMainVideoMode> getViewHolder(View view) {
            return new LayoutDrawerItemViewHolder(view);
        }

        @Override
        public void onViewDetachedFromWindow(@NonNull ViewHolder holder) {
            if (holder instanceof LayoutDrawerItemViewHolder) {
                LayoutDrawerItemViewHolder viewHolder = (LayoutDrawerItemViewHolder) holder;
                viewHolder.position = -1;
            }
        }
    }

    private static final int colorRounded = 0xffFC5B3F;
    private int selectPosition;
    private int qb_px_12;

    public class LayoutDrawerItemViewHolder extends SuperRecyclerAdapter.ViewHolder<NewMainVideoMode> {
        @BindView(R.id.icon)
        ImageView icon;
        @BindView(R.id.layoutBg)
        RoundedImageView layoutBg;

        LayoutDrawerItemViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
            layoutBg.setOnClickListener(v -> {
                if (mode == null || onLayoutDrawerListener == null) return;
                onLayoutDrawerListener.onLayoutDrawerClick(mode);
            });
        }

        @Override
        public void setPosition(int position) {
            super.setPosition(position);
            if (position == selectPosition) {
                layoutBg.setBorderColor(colorRounded);
            } else {
                layoutBg.setBorderColor(0xFF000000);
            }
        }

        @Override
        public void setData(NewMainVideoMode mode) {
            super.setData(mode);
            if (mode.contentType == -3) {
                BannerMode bannerMode = mode.bannerMode;
                if (bannerMode == null) return;
                GlideUtils.displayOfUrl(activity, icon, bannerMode.getImageUrl());
            }
        }
    }

    public interface OnLayoutDrawerListener {
        void onChangeOpen(boolean open);

        void onLayoutDrawerClick(NewMainVideoMode mode);

        void onContentOffset(float offset);
    }
}
